<?php

use Biz\cache\wiki\Wiki;
use Biz\model\GzDesignWeekActivity\GzDesignWeekActivityModel;
use Biz\model\GzDesignWeekActivity\GzDesignWeekActivityWinnerListModel;
use Doraemon\model\AdmUserType;
use Doraemon\model\Phone;

class DrawAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    /**
     * @throws \Exception
     */
    public function main()
    {
        $currentUid = $this->uid;
        //活动结束
        if (date('Ymd') >= GzDesignWeekActivityModel::END_TIME) {
            throw new Exception('活动已结束！');
        }

        if ($currentUid == 0) {
            throw new Exception('参数错误');
        }

        \Doraemon\tools\Log::info('GzDesignWeekActivity/Draw', compact('currentUid'));

        //禁止重复提交
        $lock = Wiki::isLock('gz_design_week_activity'.$currentUid, 3);
        if ($lock) {
            throw new \Exception('操作过于频繁，请稍后再试');
        }
        //查询是否已经获奖
        $isWinner = GzDesignWeekActivityWinnerListModel::isWinnerByUid($currentUid);

        //活动期内，每人最多1次中奖机会
        if ($isWinner > 0) {
            throw new Exception("您已中奖，请耐心等待奖品发放");
        }

        //查询今天是否已经参与抽奖
        $todayIsDraw = GzDesignWeekActivityWinnerListModel::toDayIsDrawByUid($currentUid);
        if ($todayIsDraw > 0) {
            throw new Exception("每天仅可抽奖一次，明天再来哦");
        }

        //反垃圾处理
        $aMemberQualityInfo = AdmUserType::getByUid($currentUid);
        $aMemberPhone = Phone::getByUid($currentUid);
         if (AdmUserType::B1 == $aMemberQualityInfo['quality'] || trim($aMemberQualityInfo['identity_mark'] ?? '') == '批量注册' || 0 === strpos($aMemberPhone['hidden_phone'],
                 '142')) {
            
             GzDesignWeekActivityWinnerListModel::insertData([
                 'uid' => $currentUid,
                 'type' => GzDesignWeekActivityWinnerListModel::Type_2,
                 'day' => date('Ymd'),
             ]);
             // throw new Exception('因违反好好住社区规范，暂时不能参与抽奖！');
             throw new Exception('未中奖');
         }
        \Doraemon\tools\Log::info('广州设计周活动反垃圾', compact('aMemberQualityInfo', 'aMemberPhone'));
        //查询是否完成全部任务
        $taskAllComplete = GzDesignWeekActivityModel::countCompleteTaskByUid($currentUid);
        if ($taskAllComplete < 4) {
            throw new Exception('获取所有经验值方可抽奖哦');
        }
        //进入抽奖环节

        //判断当日是否还有奖品
        $winnerCount = GzDesignWeekActivityWinnerListModel::getWinnerCount();
        if ($winnerCount >= GzDesignWeekActivityWinnerListModel::getWinnerLimit() || $winnerCount>=60) {
            GzDesignWeekActivityWinnerListModel::insertData([
                        'uid' => $currentUid,
                        'type' => GzDesignWeekActivityWinnerListModel::Type_2,
                        'day' => date('Ymd'),
                    ]);
            throw new Exception("未中奖");
        }

        //抽奖
        $luckDrawNum = rand(1, 10);//每个人的中奖概率10%w 
        //活动已经开始才可以中奖
        if ($luckDrawNum == 6 && date('Ymd')>= GzDesignWeekActivityWinnerListModel::START_TIME) {
            GzDesignWeekActivityWinnerListModel::insertData([
                'uid' => $currentUid,
                'type' => GzDesignWeekActivityWinnerListModel::TYPE_1,
                'day' => date('Ymd'),
            ]);
        } else {
            GzDesignWeekActivityWinnerListModel::insertData([
                'uid' => $currentUid,
                'type' => GzDesignWeekActivityWinnerListModel::Type_2,
                'day' => date('Ymd'),
            ]);
            throw new Exception("未中奖");
        }

        $this->setSuccess([], self::API_CODE_SUCCES);

    }
}